package com.flink.datastreamapi.transformation;


import com.flink.datastreamapi.bean.WaterSensor;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

public class TransMap {

    public static void main(String[] args) throws Exception {

        //执行环境、
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        //设置并行度
        env.setParallelism(1);
        //获取数据源--从集合
        DataStreamSource<WaterSensor> dataStreamSource = env.fromElements(
                new WaterSensor("sensor_1", 1l, 1),
                new WaterSensor("sensor_2", 2l, 2));
        //数据转换 : 输入WaterSensor对象--->输出对象的id
        //方式一 ： 匿名内部类的方式
        SingleOutputStreamOperator<String> map = dataStreamSource.map(new MapFunction<WaterSensor, String>() {


            @Override
            public String map(WaterSensor value) throws Exception {
                return value.id;
            }
        });

       // SingleOutputStreamOperator<String> map1 = dataStreamSource.map(value -> value.id);


        //方式二 传入MapFunction实现类
        //SingleOutputStreamOperator<String> map = dataStreamSource.map(new CustomMap());


        //输出
        map.print();


        env.execute();


    }


    //定义MapFunction接口的实现类
    public static class CustomMap implements MapFunction<WaterSensor,String>{

        @Override
        public String map(WaterSensor value) throws Exception {
            return value.id;
        }
    }

}
